package xiaocao.learn.java.io.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

public class FileLockDemo {
	
	public static void main(String[] args) throws Exception {
		FileOutputStream fos = new FileOutputStream("d://in.txt");
		FileLock fl = fos.getChannel().tryLock();
		//tryLock(long position, long size, booean shared)用来锁取文件的一部分
		if(fl != null) {
			System.out.println("Locked File");
			TimeUnit.MILLISECONDS.sleep(100);
			fl.release();
			System.out.println("Release Lock");
		}
		fos.close();
	}

}
